package com.boang.controller.venues;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boang.entity.PO.Venue;
import com.boang.mapper.VenueMapper;
import com.boang.resp.Result;
import com.boang.service.VenueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
@author:22567
@date:2025/3/10 16:32
*/
@Tag(name = "场地管理")
@RestController
@RequestMapping("/venue")
public class VenueController {
    @Autowired
    private VenueService venueService;

    @Autowired
    private VenueMapper venueMapper;

    //-------------------添加--------------------
    @Operation(summary = "添加场地", description = "   `name` v'场地名称'`type`  '类型（如：篮球场、会议室） `capacity`  '容纳人数' AVAILABLE（可用）/UNAVAILABLE（不可用）")
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody Venue venue) {
        return Result.ok(venueService.save(venue));
    }

    //-------------------修改--------------------
    @Transactional
    @Operation(summary = "修改场地")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody Venue venue) {
        return Result.ok(venueService.updateById(venue));
    }

    //-------------------删除--------------------
    @Operation(summary = "删除场地")
    @DeleteMapping("/remove")
    public Result<Boolean> remove(int id) {
        return Result.ok(venueService.removeById(id));
    }
    //-------------------查询--------------------

    @Operation(summary = "查询所有可用场地")
    @GetMapping("/available")
    public Result<List<Venue>> available() {
        return Result.ok(venueMapper.selectAvailableVenues());

    }
    @Operation(summary = "查询所有场地")
    @GetMapping("/all")
    public Result<List<Venue>> all() {
        return Result.ok(venueService.list());
    }

    @Operation(summary = "条件查询场地")
    @PostMapping("page")
    public IPage<Venue> page(@RequestBody Venue venue) {
        QueryWrapper<Venue> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.lambda()
                  .like(venue.getName() != null, Venue::getName, venue.getName())
                .like(venue.getType() != null, Venue::getType, venue.getType())
                .eq(venue.getCapacity() != 0, Venue::getCapacity, venue.getCapacity())
                .eq(venue.getStatus() != null, Venue::getStatus, venue.getStatus());

        if (venue.getPageNum() < 1) venue.setPageNum(1);
        if (venue.getPageSize() < 1 || venue.getPageSize() > 100) venue.setPageSize(10);
        Page<Venue> page = new Page<>(venue.getPageNum(), venue.getPageSize());
        return venueService.page(page, QueryWrapper);
    }
}
